package com.eight.homepage.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eight.common.pojo.TbArticle;
import com.eight.common.pojo.TbBottomArticle;
import com.eight.common.pojo.TbZone;
import com.eight.common.vo.Result;
import com.eight.homepage.dao.ArticleDao;
import com.eight.homepage.dao.BottomArticleDao;
import com.eight.homepage.service.BottomArticleService;
import com.eight.homepage.vo.BottomArticleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 向文豪
 * @program: eight-education-parent
 * @description:
 * @date 2023/5/30 10:22
 */
@Service
public class BottomArticleServiceImpl implements BottomArticleService {
    @Autowired
    private BottomArticleDao bottomArticleDao;

    @Autowired
    private ArticleDao articleDao;
    @Override
    public Page<TbBottomArticle> findPage(Integer currentSize, Integer pageSize, BottomArticleVo bottomArticleVo) {
     Page<TbBottomArticle> page = new Page<>();
        QueryWrapper<TbBottomArticle> wrapper = new QueryWrapper<>();

        if(StringUtils.hasText(bottomArticleVo.getName())){
            wrapper.like("name",bottomArticleVo.getName());
        }
        if(bottomArticleVo.getIsDisable()!=null){
            wrapper.eq("is_disable",bottomArticleVo.getIsDisable());
        }

        wrapper.eq("parent_id","0");
        Page<TbBottomArticle> articlePage = bottomArticleDao.selectPage(page, wrapper);
        List<TbBottomArticle> records = articlePage.getRecords();
        List<TbBottomArticle> tbBottomArticles = bottomArticleDao.selectList(null);

        for(TbBottomArticle item : records){
            List<TbBottomArticle> children = findChildren(item, tbBottomArticles);
            item.setChildren(children);
        }

        return articlePage;
    }

    @Override
    public int updateStatus(String id, Integer isDisable) {
        TbBottomArticle bottomArticle = new TbBottomArticle();
        bottomArticle.setId(id);
        bottomArticle.setIsDisable(isDisable);
        bottomArticle.setGmtModified(LocalDateTime.now());
        int i = bottomArticleDao.updateById(bottomArticle);

        return i;
    }

    @Override
    public int delete(TbBottomArticle id) {
        int i = bottomArticleDao.deleteById(id);
        return i;

    }

    @Override
    public int insert(TbBottomArticle tbBottomArticle) {


        String id = IdUtil.getSnowflakeNextIdStr();
        //文章详情添加 tb_article
        TbArticle article = new TbArticle(IdUtil.getSnowflakeNextIdStr(), LocalDateTime.now(), LocalDateTime.now(), 0, id, tbBottomArticle.getName(), null, null);
        articleDao.insert(article);


        TbBottomArticle bottomArticle = new TbBottomArticle(id, tbBottomArticle.getName(), 0, 0, tbBottomArticle.getParentId(), LocalDateTime.now(), LocalDateTime.now(), new ArrayList<TbBottomArticle>());
        int insert = bottomArticleDao.insert(bottomArticle);
        return insert;
    }

    @Override
    public int update(TbBottomArticle tbBottomArticle) {
        int i = bottomArticleDao.updateById(tbBottomArticle);
        return i;
    }


    private List<TbBottomArticle> findChildren(TbBottomArticle item, List<TbBottomArticle> childrens){
        ArrayList<TbBottomArticle> list = new ArrayList<>();
        for (TbBottomArticle children : childrens) {
            if(item.getId().equals(children.getParentId())){
                list.add(children);
            }
        }
        for (TbBottomArticle eduSubject : list) {
            eduSubject.setChildren(findChildren(eduSubject,childrens));
        }
        return list;
    }
}
